package com.becca.cake.core.task.impl;

import java.util.Date;
import java.util.Map;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;

import com.becca.cake.core.constant.CommonConstants;
import com.becca.cake.core.task.Task;
import com.becca.cake.service.orders.IOrderService;
import com.becca.cake.util.DateUtils;

/**
 * 订单信息统计邮件通知
 * @copyright  尊星(上海)信息科技有限公司
 * @author hejinguo
 * @date 2016年7月18日 上午11:24:15
 */
public class OrderCountMailTask implements Task {
    private static final Logger logger    = LoggerFactory.getLogger(OrderCountMailTask.class);

    private JavaMailSender      javaMailSender;

    private IOrderService       orderService;

    private boolean             executing = false;

    @Override
    public void doTask() {
        if (executing) {
            return;
        }
        executing = true;
        logger.error("order  count task start...");
        //step1:获取前一天4点前和4点后的订单信息
        Map<String, Object> orderMap = null;
        try {
            orderMap = this.orderService.statisticsOrderReport();
            String orderReportUrl = String.valueOf(orderMap.get("orderReportUrl"));
            logger.error("统计报表邮件链接地址为:" + orderReportUrl);
            if (StringUtils.isNotEmpty(orderReportUrl)) {
                String templateHtml = "<p>Dear all:</p><p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;MM统计报表已生成,请点击如下链接查看</p><p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="
                                      + orderReportUrl + ">" + orderReportUrl + "</a></p>";
                //cc MAIL
                logger.error("统计订发送邮件!");
                String[] toMailArray = CommonConstants.EMAIL_TO_MAIL.split(",");
                sendHtmlEmail(toMailArray, templateHtml);
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("订单统计邮件发送错误!", e);
        }
        executing = false;
        logger.error("order  count task end...");
    }

    /**
     * 
     * 邮件发送的方法类采用springmail注入
     * @author wangyuxin
     * @version 
     * @throws MessagingException 
     */
    public void sendHtmlEmail(String[] toMail, String htmlText) throws Exception {
        logger.error("开始准备发送邮件!");
        Date nightBeforDate = DateUtils.getSomeDayDate(-1);
        String nightBeforDateString = DateUtils.date2StrDate(nightBeforDate);
        MimeMessage msg = javaMailSender.createMimeMessage();
        MimeMessageHelper helper;
        helper = new MimeMessageHelper(msg, true, "UTF-8");
        helper.setFrom(CommonConstants.EMAIL_FROM_ADDRESS); // 邮件发送地址,这里必须和xml里的邮件地址相同一致  
        helper.setTo(toMail); // 邮件接收地址  
        helper.setSubject(nightBeforDateString + "交易订单统计"); // 主题  
        helper.setText(htmlText, true); // 邮件内容，注意加参数true，表示启用html格式  
        logger.error("发送邮件开始!");
        javaMailSender.send(msg);
        logger.error("发送邮件结束!");
        logger.error(nightBeforDateString + "交易订单统计:邮件发送成功!");
    }

    public void setJavaMailSender(JavaMailSender javaMailSender) {
        this.javaMailSender = javaMailSender;
    }

    public JavaMailSender getJavaMailSender() {
        return javaMailSender;
    }

    public void setOrderService(IOrderService orderService) {
        this.orderService = orderService;
    }

    public IOrderService getOrderService() {
        return orderService;
    }

}
